package com.cssy.xxkj.logic.web;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cssy.base.web.BaseController;
import com.cssy.base.web.BodyMsg;
import com.cssy.xxkj.basic.entity.BasicStorageInfoTestDO;
import com.cssy.xxkj.basic.service.IBasicUtilService;
import com.cssy.xxkj.logic.contants.BillStatusUtil;
import com.cssy.xxkj.logic.entity.LogicOutsideBoundDO;
import com.cssy.xxkj.logic.entity.LogicOutsideBoundItemDO;
import com.cssy.xxkj.logic.entity.LogicOutsideStockItemDO;
import com.cssy.xxkj.logic.service.ILogicOutsideBoundService;
import com.cssy.xxkj.logic.service.ILogicOutsideStockService;
import com.plat.exception.SyBaseException;
import com.plat.helper.JqGridHandler;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.GlobalResLoader;
import com.plat.util.OperationFlag;
import com.plat.util.SessionUtil;
import com.plat.util.json.OutJsonGrid;

@Controller
@RequestMapping("/logic/outsideout")
public class OutSideBoundController extends BaseController<LogicOutsideBoundDO> {
	
	private static final String MANAGER = "/xxkj/logic/outsideout/OutSideOutManager";
	private static final String EIDT = "/xxkj/logic/outsideout/OutSideOutOperation";
	
	@Resource(name = "logicOutsideBoundService")
	private ILogicOutsideBoundService logicOutsideBoundService;
	
	@Resource(name = "logicOutsideStockService")
	private ILogicOutsideStockService logicOutsideStockService;
	
	@Resource(name = "basicUtilService")
	private IBasicUtilService basicUtilService;
	
	@Override
	public String manager(HttpServletRequest request, Model model)
			throws Exception {
		//入库仓
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		List<BasicStorageInfoTestDO> list = basicUtilService.resultStorage(scs);
		model.addAttribute("wh",list);
		return MANAGER;
	}

	@Override
	public void list(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JqGridHandler hander=new JqGridHandler(request);
		SearchConditions payScs = new SearchConditions();
		hander.getWheres(payScs);
		//payScs.setRemovedProperty(null);
		payScs.setOrderBy("bizCreatorDate desc");
		SearchResult result= logicOutsideBoundService.findListByExample(payScs);
		OutJsonGrid out=new OutJsonGrid(result);
		out.printJSon(response);
	}

	@Override
	public String operationForm(HttpServletRequest request, @PathVariable String id,
			@PathVariable String flag, Model model) throws Exception {
		if (OperationFlag.ADD.equals(flag)) {
			// 新增单据时操作
			model.addAttribute("flag", "ADD");
		} else {
			//修改单据时操作
			LogicOutsideBoundDO sbond = logicOutsideBoundService.findListById(id);
			model.addAttribute("sbond", sbond);
		}
		//查询仓库
		SearchConditions sfScs = new SearchConditions();
		sfScs.setRemovedProperty(null);
		List<BasicStorageInfoTestDO>  sgfList = basicUtilService.resultStorage(sfScs);
		model.addAttribute("sgfList", sgfList);
		
		return EIDT;
	}

	@Override
	public Map<String, Object> save(HttpServletRequest request,
			LogicOutsideBoundDO curAP, BindingResult result, Model model)
			throws Exception {
		BodyMsg bodyMap = BodyMsg.getInstance();
		try{
			String itemdata = request.getParameter("dataitem");
			if(StringUtils.isBlank(itemdata)){
				bodyMap.isFail();
				bodyMap.configWebInfor("入库物料列表不能为空！");
				return bodyMap.modelMap;
			}
			logicOutsideBoundService.outSideOutSave(curAP, request);
			bodyMap.isSuccess();
			bodyMap.configWebInfor(GlobalResLoader.getOpsuccess());
		}catch(Exception e){
			e.printStackTrace();
			throw new SyBaseException(GlobalResLoader.getOpfailed()+ e.getMessage());
		}
		return bodyMap.modelMap;
	}

	@Override
	public Map<String, Object> delete(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 查询委外出库的item集合
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/gridItems")
	public void gridItems(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String outSideOutId = request.getParameter("outSideOutId");
		if(StringUtils.isNotBlank(outSideOutId)){ 
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("outSideOutId", outSideOutId);
			List<LogicOutsideBoundItemDO> boundItemList = logicOutsideBoundService.findItemListByMap(paramMap);
			OutJsonGrid out = new OutJsonGrid(boundItemList);
			out.printJSon(response);
		}
	}
	
	/**
	 * 根据委外入库单id查询委外入库单的item
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/outSideInItem")
	public void outSideInItem(HttpServletRequest request, HttpServletResponse response)throws Exception{
		String outSideInId = request.getParameter("outSideInId");
		if(StringUtils.isNotBlank(outSideInId)){
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("outSideInId", outSideInId);
			List<LogicOutsideStockItemDO> outSideInItemList = logicOutsideStockService.findItemListByMap(paramMap);
			if(outSideInItemList==null){
				outSideInItemList = new ArrayList<LogicOutsideStockItemDO>();
			}
			List<LogicOutsideBoundItemDO> sideBoundItemList = new ArrayList<LogicOutsideBoundItemDO>();
			for(LogicOutsideStockItemDO outSideInItem : outSideInItemList){
				if(outSideInItem==null){
					continue;
				}
				LogicOutsideBoundItemDO outSideOutItem = new LogicOutsideBoundItemDO();
				outSideOutItem.setZd003MaterId(outSideInItem.getZd003MaterId());
				outSideOutItem.setZd004MaterCode(outSideInItem.getZd004MaterCode());
				outSideOutItem.setZd005MaterName(outSideInItem.getZd005MaterName());
				outSideOutItem.setZd006MaterType(outSideInItem.getZd006MaterType());
				outSideOutItem.setZd007MaterSpec(outSideInItem.getZd007MaterSpec());
				outSideOutItem.setZd008MaterModel(outSideInItem.getZd008MaterModel());
				outSideOutItem.setZd009MaterBrand(outSideInItem.getZd009MaterBrand());
				outSideOutItem.setZd010MaterSize(outSideInItem.getZd010MaterSize());
				outSideOutItem.setZd011MaterColor(outSideInItem.getZd011MaterColor());
				outSideOutItem.setZd012MaterQuanlity(outSideInItem.getZd012MaterQuanlity());
				outSideOutItem.setZd013Unit(outSideInItem.getZd013Unit());
				outSideOutItem.setZd014Price(outSideInItem.getZd014Price());
				outSideOutItem.setZd014TaxPrice(outSideInItem.getZd014TaxPrice());
				BigDecimal inQuantity = outSideInItem.getZd018InQuantity()==null?BigDecimal.ZERO:outSideInItem.getZd018InQuantity();
				outSideOutItem.setZd018InQuantity(inQuantity);
				BigDecimal hasOutQuantity = outSideInItem.getZd018HasOutQuantity()==null?BigDecimal.ZERO:outSideInItem.getZd018HasOutQuantity();
				outSideOutItem.setZd018HasOutQuantity(hasOutQuantity);
				BigDecimal noOutQuantity = outSideInItem.getZd018NoOutQuantity()==null?BigDecimal.ZERO:outSideInItem.getZd018NoOutQuantity();
				outSideOutItem.setZd018NoOutQuantity(inQuantity.subtract(noOutQuantity));
				sideBoundItemList.add(outSideOutItem);
			}
			OutJsonGrid out = new OutJsonGrid(sideBoundItemList);
			out.printJSon(response);
		}
	}
	
	@RequestMapping(value = "/updateState")
	@ResponseBody
	public Map<String,String> updateState(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		Map<String, String> msgMap=new HashMap<String, String>();
		try{
			String outSideOutId = request.getParameter("outSideOutId");
			if(StringUtils.isNotBlank(outSideOutId)){
				LogicOutsideBoundDO outSideBound = logicOutsideBoundService.findListById(outSideOutId);
				if(outSideBound==null||StringUtils.isBlank(outSideBound.getId())){
					msgMap.put("msg", "ERROR");
				}else{
					outSideBound.setZd001Status(BillStatusUtil.OUTSIDEOUT_STATUS_THREE);
					logicOutsideBoundService.update(outSideBound, SessionUtil.getUser(request));
					msgMap.put("msg", "SUCCESS");
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			msgMap.put("msg", "ERROR");
		}
		return msgMap;
	}
}
